package com.tanhua.admin.controller;

import com.tanhua.admin.service.CommentService;
import com.tanhua.admin.service.ManagerService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manage")
@Slf4j
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @Autowired
    private CommentService commentService;

    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = managerService.findAllUsers(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 根据id查询用户详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/users/{userId}")
    public ResponseEntity findUserById(@PathVariable("userId") Long userId) {
        UserInfo userInfo = managerService.findUserById(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 查询指定用户发布的所有视频列表
     */
    @GetMapping("/videos")
    public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 Long uid) {
        PageResult result = managerService.findAllVideos(page, pagesize, uid);
        return ResponseEntity.ok(result);
    }

    //查询动态
    @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   Long uid, Integer state) {
        PageResult result = managerService.findAllMovements(page, pagesize, uid, state);
        return ResponseEntity.ok(result);
    }

    //用户冻结
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map params) {
        Map map = managerService.userFreeze(params);
        return ResponseEntity.ok(map);
    }

    //用户解冻
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map params) {
        Map map = managerService.userUnfreeze(params);
        return ResponseEntity.ok(map);
    }

    //后台根据id查询动态详情
    @GetMapping("/messages/{id}")
    public ResponseEntity messagesInfo(@PathVariable("id") String id) {
        MovementsVo vo = managerService.findMovementById(id);
        return ResponseEntity.ok(vo);
    }

    /**
     * 分页查询动态评论列表
     *
     * @param page
     * @param pagesize
     * @param messageID
     * @return
     */
    @GetMapping("/messages/comments")
    public ResponseEntity commentsList(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize,
                                       String messageID) {
        log.info("动态id", messageID);
        PageResult pr = commentService.findListById(messageID, page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 动态放行
     *
     * @param movementIds
     * @return
     */
    @PostMapping("/messages/pass")
    public ResponseEntity psaaMovements(@RequestBody String[] movementIds) {
        //创建集合用来封装动态Id
        List<String> movementId = new ArrayList<>();
        //判断封装动态Id的数组是否不为空
        if (movementIds != null) {
            //遍历数组获取数组中的动态Id
            for (String passMovementIds : movementIds) {
                //封装到集合里
                movementId.add(passMovementIds);
            }
        }
        //调用service根据动态Id放行

        return managerService.passMovements(movementId);
    }

    /**
     * 动态拒绝
     *
     * @param movementIds
     * @return
     */
    @PostMapping("/messages/reject")
    public ResponseEntity rejectMovements(@RequestBody String[] movementIds) {
        //创建集合用来封装动态Id
        List<String> movementId = new ArrayList<>();
        //判断封装动态Id的数组是否不为空
        if (movementIds != null) {
            //遍历数组获取数组中的动态Id
            for (String passMovementIds : movementIds) {
                //封装到集合里
                movementId.add(passMovementIds);
            }
        }
        //调用service根据动态Id放行
        return managerService.rejectMovements(movementId);
    }

    /**
     * 动态置顶
     *
     * @param id
     * @return
     */
    @PostMapping("/messages/{id}/top")
    public ResponseEntity top(@PathVariable("id") String id) {
       String message= managerService.top(id);
        return ResponseEntity.status(HttpStatus.OK).body(message);
    }
    @PostMapping("/messages/{id}/untop")
    public ResponseEntity unTop(@PathVariable("id") String id){
        String message= managerService.untop(id);
        return ResponseEntity.status(HttpStatus.OK).body(message);
    }
}
